//
//  MapImageViewController.m
//  SXH
//
//  Created by 房 国生 on 15/7/6.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "MapImageViewController.h"
#import "SKIndoorMapView.h"
#import "CommonService.h"
#import "SpotEntity.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "YYAnimationIndicator.h"
#import "SpotDetailViewController.h"

#import "RoadLineEntity.h"

@interface MapImageViewController ()<MBProgressHUDDelegate, SKIndoorMapViewDelegate>
{
    MBProgressHUD *HUD;
    YYAnimationIndicator *indicator;
}

@property (nonatomic, strong) IBOutlet UIButton *btnSpot;
@property (nonatomic, strong) IBOutlet UIButton *btnSpotText;
@property (nonatomic, strong) IBOutlet UIButton *btnBusiness;
@property (nonatomic, strong) IBOutlet UIButton *btnBusinessText;
@property (nonatomic, strong) IBOutlet UIButton *btnToilet;
@property (nonatomic, strong) IBOutlet UIButton *btnToiletText;
@property (nonatomic, strong) IBOutlet UIButton *btnOther;
@property (nonatomic, strong) IBOutlet UIButton *btnOtherText;
@property (nonatomic, strong) IBOutlet UIButton *btnRoute;
@property (nonatomic, strong) IBOutlet UIButton *btnRouteText;
@property (nonatomic, strong) SKIndoorMapView *indoorView;
@property (nonatomic, strong) IBOutlet UIButton *btnBack;
@property (nonatomic, strong) IBOutlet UIView *viewMenu;

@property (nonatomic, strong) IBOutlet UIView *viewPopMenu;
@property (nonatomic, strong) IBOutlet UIPickerView *pickerView;

@property (nonatomic, strong) RoadLineEntity *mRoadLineEntity;

@end

@implementation MapImageViewController
@synthesize btnBusiness, btnBusinessText, btnOther, btnOtherText;
@synthesize btnRoute, btnRouteText, btnSpot, btnSpotText, btnToilet;
@synthesize btnToiletText;
@synthesize indoorView, viewMenu, btnBack, viewPopMenu,pickerView;
@synthesize parkEntity, mutableArray, mutableArrayRoad;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"瘦西湖";
    
    [self.viewPopMenu setFrame:CGRectMake(0, ScreenHeight,ScreenWidth, viewPopMenu.frame.size.height)];
    [self.view addSubview:self.viewPopMenu];
    
    indoorView = [[SKIndoorMapView alloc] initWithIndoorMapImageName:parkEntity
                                                               Frame:self.view.bounds
                                                               Array:mutableArray];
    indoorView.mapImageViewDelegate = self;
    
    [self.view addSubview:indoorView];
    
    [self initBottomView:0];
    
    [self.view bringSubviewToFront:viewMenu];
    [self.view bringSubviewToFront:btnBack];
    
    [self.viewPopMenu setFrame:CGRectMake(0, ScreenHeight,ScreenWidth, viewPopMenu.frame.size.height)];
    [self.view addSubview:self.viewPopMenu];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)doSpotAction:(id)sender {
    [self initBottomView:0];
    [indoorView showHotPointByType:@"1"];
}

- (IBAction)doBusinessAction:(id)sender {
    [self initBottomView:1];
    [indoorView showHotPointByType:@"2"];
}

- (IBAction)doToiletAction:(id)sender {
    [self initBottomView:2];
    [indoorView showHotPointByType:@"3"];
}

- (IBAction)doRouteAction:(id)sender {
    [self initBottomView:4];
    
    if ([mutableArrayRoad count] == 0) {
        ALERT_ERR_MSG(@"没有推荐线路");
    } else {
        [self popSpotMenu];
    }
}

- (void)initBottomView:(int) tab {
    
    if (tab == 0) {
        
        [btnSpot setBackgroundImage:[UIImage imageNamed:@"地图_13"] forState:UIControlStateNormal];
        [btnBusiness setBackgroundImage:[UIImage imageNamed:@"地图_14"] forState:UIControlStateNormal];
        [btnToilet setBackgroundImage:[UIImage imageNamed:@"地图_15"] forState:UIControlStateNormal];
        [btnOther setBackgroundImage:[UIImage imageNamed:@"地图_16"] forState:UIControlStateNormal];
        [btnRoute setBackgroundImage:[UIImage imageNamed:@"地图_17"] forState:UIControlStateNormal];
        
        btnSpotText.titleLabel.textColor = [UIColor colorWithRed:0.55 green:0.77 blue:0.2 alpha:1];
        btnBusinessText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnToiletText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnOtherText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnRouteText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        
    } else if (tab == 1) {
        [btnSpot setBackgroundImage:[UIImage imageNamed:@"地图_03"] forState:UIControlStateNormal];
        [btnBusiness setBackgroundImage:[UIImage imageNamed:@"地图_05"] forState:UIControlStateNormal];
        [btnToilet setBackgroundImage:[UIImage imageNamed:@"地图_15"] forState:UIControlStateNormal];
        [btnOther setBackgroundImage:[UIImage imageNamed:@"地图_16"] forState:UIControlStateNormal];
        [btnRoute setBackgroundImage:[UIImage imageNamed:@"地图_17"] forState:UIControlStateNormal];
        
        btnSpotText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnBusinessText.titleLabel.textColor = [UIColor colorWithRed:0.55 green:0.77 blue:0.2 alpha:1];
        btnToiletText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnOtherText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnRouteText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
    } else if (tab == 2) {
        [btnSpot setBackgroundImage:[UIImage imageNamed:@"地图_03"] forState:UIControlStateNormal];
        [btnBusiness setBackgroundImage:[UIImage imageNamed:@"地图_14"] forState:UIControlStateNormal];
        [btnToilet setBackgroundImage:[UIImage imageNamed:@"地图_07"] forState:UIControlStateNormal];
        [btnOther setBackgroundImage:[UIImage imageNamed:@"地图_16"] forState:UIControlStateNormal];
        [btnRoute setBackgroundImage:[UIImage imageNamed:@"地图_17"] forState:UIControlStateNormal];
        
        btnSpotText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnBusinessText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnToiletText.titleLabel.textColor = [UIColor colorWithRed:0.55 green:0.77 blue:0.2 alpha:1];
        btnOtherText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnRouteText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
    }else if (tab == 3) {
        [btnSpot setBackgroundImage:[UIImage imageNamed:@"地图_03"] forState:UIControlStateNormal];
        [btnBusiness setBackgroundImage:[UIImage imageNamed:@"地图_14"] forState:UIControlStateNormal];
        [btnToilet setBackgroundImage:[UIImage imageNamed:@"地图_15"] forState:UIControlStateNormal];
        [btnOther setBackgroundImage:[UIImage imageNamed:@"地图_09"] forState:UIControlStateNormal];
        [btnRoute setBackgroundImage:[UIImage imageNamed:@"地图_17"] forState:UIControlStateNormal];
        
        btnSpotText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnBusinessText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnToiletText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnOtherText.titleLabel.textColor = [UIColor colorWithRed:0.55 green:0.77 blue:0.2 alpha:1];
        btnRouteText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
    }else if (tab == 4) {
        [btnSpot setBackgroundImage:[UIImage imageNamed:@"地图_03"] forState:UIControlStateNormal];
        [btnBusiness setBackgroundImage:[UIImage imageNamed:@"地图_14"] forState:UIControlStateNormal];
        [btnToilet setBackgroundImage:[UIImage imageNamed:@"地图_15"] forState:UIControlStateNormal];
        [btnOther setBackgroundImage:[UIImage imageNamed:@"地图_16"] forState:UIControlStateNormal];
        [btnRoute setBackgroundImage:[UIImage imageNamed:@"地图_11"] forState:UIControlStateNormal];
        
        btnSpotText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnBusinessText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnToiletText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnOtherText.titleLabel.textColor = [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:1];
        btnRouteText.titleLabel.textColor = [UIColor colorWithRed:0.55 green:0.77 blue:0.2 alpha:1];
    }
}

//- (void) getSpotData
//{
//    CommonService *service = [[CommonService alloc] init];
//    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
//                           @"1", @"parkid", nil];
//    
//    [service getNetWorkData:param
//                       Path:SPOTPATH
//                  Successed:^(id entity) {
//                      NSDictionary *dic = (NSDictionary *)entity;
//                      NSArray *array = [dic objectForKey:@"attraction"];
//                      for (NSDictionary* attributes in array) {
//                          SpotEntity *entity = [[SpotEntity alloc] init];
//                          entity = [entity initWithAttributes:attributes];
//                          [ [AppDelegate instance].arraySpot addObject:entity];
//                      }
//                      
//                      indoorView = [[SKIndoorMapView alloc] initWithIndoorMapImageName:parkEntity
//                                                                                 Frame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)
//                                                                                 Array:[AppDelegate instance].arraySpot];
//                      [indicator stopAnimationWithLoadText:@"加载完成..." withType:YES];
//                  } Failed:^(NSInteger errorCode, NSString *message) {
//                      [indicator stopAnimationWithLoadText:@"加载完成..." withType:YES];
//                  }];
//}

- (void) pushAction:(SpotEntity *)entity ArraySpot:(NSMutableArray *)array Index:(NSInteger)index{
    
//    NSMutableArray *array = [[NSMutableArray alloc] init];
//    
//    for (int i = 0; i < [mutableArray count]; i++) {
//        SpotEntity *entity = [mutableArray objectAtIndex:i];
//        if ([entity.ptype isEqualToString:@"1"] == YES) {
//            [array addObject:entity];
//        }
//    }
    //
    SpotDetailViewController *vc = [[SpotDetailViewController alloc] init];
    vc.musicArray = array;
    vc.musicArrayNumber = index;
    vc.parkEntity = parkEntity;
    vc.musicArray = mutableArray;
    vc.entity = entity;
    
    [self.navigationController pushViewController:vc animated:YES];
}

//----------------------------------------------------------------------------------------------------------
// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [mutableArrayRoad count];
}

// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 180;
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //路线图
    _mRoadLineEntity = [mutableArrayRoad objectAtIndex:row];
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    RoadLineEntity *entity = [mutableArrayRoad objectAtIndex:row];
    return entity.title;
}

- (void)popSpotMenu {
    [UIView animateWithDuration:.35 animations:^{
        //self.viewPopMenu.backgroundColor = RGBACOLOR(0, 0, 0, .9);
        self.viewPopMenu.alpha = 1.0;
        [UIView animateWithDuration:.25 animations:^{
            [self.viewPopMenu setFrame:CGRectMake(self.viewPopMenu.frame.origin.x, ScreenHeight-self.viewPopMenu.frame.size.height, self.viewPopMenu.frame.size.width, self.viewPopMenu.frame.size.height)];
        }];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hidenPopMenu {
    [UIView animateWithDuration:.35 animations:^{
        [self.viewPopMenu setFrame:CGRectMake(0, ScreenHeight,ScreenWidth, viewPopMenu.frame.size.height)];
        self.viewPopMenu.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            //[self.viewListSpot removeFromSuperview];
        }
    }];
}

- (IBAction)tappedSelect: (id)sender {
    
    if (_mRoadLineEntity == nil) {
        if ([mutableArrayRoad count] > 0) {
            _mRoadLineEntity = [mutableArrayRoad objectAtIndex:0];
        }
    } else {
        //
        NSString *localImagePath = [CommonService addLocalRootPath:_mRoadLineEntity.img_path ParkId:parkEntity.ID];
        DebugLog(@"%@", localImagePath);
        UIImage* img = [UIImage imageWithContentsOfFile:localImagePath];
        [indoorView addRouteMap:img];
    }
    [self hidenPopMenu];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"MapImageViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"MapImageViewController"];
}
@end





